package com.icejoywoo.rss;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.icejoywoo.rss.helper.TipHelper;

/**
 * 显示url在页面顶部, 加载完成后自动消失, webview显示网页
 * @author icejoywoo
 *
 */
public class SimpleBrowser extends Activity {

	private WebView webView;
	private TextView urlText;
	private ImageButton backBtn;
	private ImageButton refreshBtn;
	private WebSettings webSettings;
	private ProgressBar progressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		log("Simple Browser is starting...");
		super.onCreate(savedInstanceState);
		// 全屏显示
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.simple_webview);
		
		refreshBtn = (ImageButton) findViewById(R.id.browser_refresh);
		refreshBtn.setAlpha(100);
		refreshBtn.setOnClickListener(new ImageButton.OnClickListener() {
			@Override
			public void onClick(View v) {
				TipHelper.vibrate(SimpleBrowser.this, 200);
				webView.reload();
			}
		});
		backBtn = (ImageButton) findViewById(R.id.browser_back);
		backBtn.setAlpha(100);
		backBtn.setOnClickListener(new ImageButton.OnClickListener() {
			// 计数退出 >=2 退出当前activity
			private int count = 0;
			@Override
			public void onClick(View v) {
				if (webView.canGoBack()) {
					TipHelper.vibrate(SimpleBrowser.this, 200);
					webView.goBack();
					count = 0;
				} else {
					toast("页面无法退后. 请再按返回键退出.");
					TipHelper.vibrate(SimpleBrowser.this, 200);
					++count;
					if (count >= 2) {
						SimpleBrowser.this.finish();
					}
				}
			}
		});
		
		webView = (WebView) findViewById(R.id.browser);
		urlText = (TextView) findViewById(R.id.browser_url);
		urlText.setBackgroundColor(Color.argb(155, 0, 0, 0)); // 半透明黑色背景
		
		webSettings = webView.getSettings();
		webSettings.setAllowFileAccess(true);//设置允许访问文件数据  
		webSettings.setJavaScriptEnabled(true);//设置支持javascript脚本  
		webSettings.setBuiltInZoomControls(true);//设置支持内建的缩放工具
		webSettings.setSupportZoom(true);//启用页面的缩放
		
		webView.setWebViewClient(new WebViewClient(){
			/**
			 * 在webview中加载内部链接, 同时更新textview的url
			 */
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				urlText.setText(url);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				urlText.setVisibility(View.GONE);
				progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				urlText.setVisibility(View.VISIBLE);
				progressBar.setVisibility(View.VISIBLE);
			}
			
		});
		
		progressBar = (ProgressBar) findViewById(R.id.progress_bar);
		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				progressBar.setProgress(newProgress);
			}

			@Override
			public void onCloseWindow(WebView window) {
				super.onCloseWindow(window);
			}
			
		});
		log("Start");
		Bundle bundle = this.getIntent().getExtras();
		String webViewUrl = bundle.getString("url");
		log(webViewUrl);
		urlText.setText(webViewUrl);
		// 加载url
		webView.loadUrl(webViewUrl);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果点击了返回键并且本页面不是第一次浏览的页面(即允许后退)
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
	         webView.goBack();//执行返回
	         return true;
	     } else if (keyCode == KeyEvent.KEYCODE_BACK && !webView.canGoBack()) {
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Just a simple log wrapper for this class
	 * @param msg the log message
	 */
	private void log(String msg) {
		Log.d("SimpleBrowser", msg);
	}
	
}
